﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns
{
    public abstract class DComponent
    {
        public abstract void operation();

    }

    public class ConcreteComponent : DComponent
    {
        public override void operation()
        {
            Console.WriteLine("具体对象的操作");
        }
    }

    /// <summary>
    /// Decorator装饰类
    /// </summary>
    public abstract class Decorator : DComponent
    {

        private DComponent component = null;

        //通过构造函数传递给被修饰者
        public Decorator(DComponent component)
        {
            this.component = component;
        }

        //委托给被修饰者执行
        public override void operation()
        {
            if (component != null)
            {
                this.component.operation();
            }
        }

    }

    public class ConcreteDecoratorA : Decorator
    {

        //定义被修饰者
        public ConcreteDecoratorA(DComponent component) : base(component)
        {
        }

        //定义自己的修饰方法
        private void method1()
        {
            Console.WriteLine("method1 修饰");
        }


        public override void operation()
        {
            this.method1();
            base.operation();
        }

    }


    public class ConcreteDecoratorB : Decorator
    {

        //定义被修饰者
        public ConcreteDecoratorB(DComponent component) : base(component)
        {
        }

        //定义自己的修饰方法
        private void method2()
        {
            Console.WriteLine("method2 修饰");
        }

        public override void operation()
        {
            base.operation();
            this.method2();
        }

    }
}
